package com.lagou.bean;

import com.lagou.service.HelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@Component
public class ConsumerComponent {

    //@Reference
    @Autowired
    private HelloService helloService;

    public String sayHello(String name, int timeToWait) throws ExecutionException, InterruptedException {
        System.out.println("===ConsumerComponent.sayHello begin...");
        String hello = helloService.sayHello(name, timeToWait);
        System.out.println("xxoo异步是指调用了服务后不需等待，可以执行其他逻辑。不过最后利用Future模式获取还是要等待以及做后面的处理");
        System.out.println("xxoo总的来说，应用层还是要等服务层执行完，只是在这段时间内可以执行其他逻辑");
        Future<Object> future = RpcContext.getContext().getFuture();
        System.out.println("===ConsumerComponent result :" + hello);
        System.out.println("===ConsumerComponent future result:" + future.get());
        System.out.println("===ConsumerComponent.sayHello end...");
//        return hello;
        return future.get().toString();
    }

}
